package com.cskaoyan.order.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.util.CookieUtil;
import com.cskaoyan.mall.commons.util.jwt.JwtTokenUtils;
import com.cskaoyan.order.dto.OrderDetailResponse;
import com.cskaoyan.order.dto.OrderListResponse;
import com.cskaoyan.order.form.CancelOrderRequest;
import com.cskaoyan.order.form.CreateOrderRequest;
import com.cskaoyan.order.form.OrderListRequest;
import com.cskaoyan.order.service.OrderCoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.security.x509.RDN;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

/**
 * @author lwq
 * @since 2022/12/03 10:39
 */
@RestController
@RequestMapping("shopping")
public class OrderController {
    @Autowired
    OrderCoreService orderCoreService;

    /**
     * 创建订单
     *
     * @param request
     * @param createOrderRequest
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author lwq
     * @since 2022/12/03 16:39
     */
    @PostMapping("order")
    public ResponseData create(HttpServletRequest request, @RequestBody CreateOrderRequest createOrderRequest) {
        // 获取userID
        String userInfo = request.getHeader("user_info");
        JSONObject jsonObject = JSON.parseObject(userInfo);
        long userId = Long.parseLong(jsonObject.get("uid").toString());
        createOrderRequest.setUserId(userId);
        String orderId = orderCoreService.createOrder(createOrderRequest);
        return new ResponseUtil<String>().setData(orderId);
    }

    /**
     * 查询用户订单
     *
     * @param request
     * @param orderListRequest
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author lwq
     * @since 2022/12/04 13:59
     */
    @GetMapping("order")
    public ResponseData queryOrder(HttpServletRequest request, OrderListRequest orderListRequest) {
        // 获取userID
        String userInfo = request.getHeader("user_info");
        JSONObject jsonObject = JSON.parseObject(userInfo);
        long userId = Long.parseLong(jsonObject.get("uid").toString());
        orderListRequest.setUserId(userId);
        OrderListResponse orderListResponse = orderCoreService.queryOrder(orderListRequest);
        return new ResponseUtil<OrderListResponse>().setData(orderListResponse);
    }

    /**
     * 查询订单详情
     *
     * @param orderId
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author lwq
     * @since 2022/12/04 14:04
     */
    @GetMapping("order/{id}")
    public ResponseData orderDetail(@PathVariable("id") String orderId) {
        OrderDetailResponse orderDetailResponse = orderCoreService.orderDetail(orderId);
        return new ResponseUtil<OrderDetailResponse>().setData(orderDetailResponse);
    }

    /**
     * 取消订单
     *
     * @param cancelOrderRequest
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author lwq
     * @since 2022/12/04 14:25
     */
    @PostMapping("cancelOrder")
    public ResponseData cancelOrder(@RequestBody CancelOrderRequest cancelOrderRequest) {
        orderCoreService.cancelOrder(cancelOrderRequest.getOrderId());
        return new ResponseUtil<String>().setData("成功", "success");
    }

    /**
     * 删除订单
     *
     * @param orderId
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author lwq
     * @since 2022/12/04 14:53
     */
    @DeleteMapping("order/{id}")
    public ResponseData deleteOrder(@PathVariable("id") String orderId) {
        orderCoreService.deleteOrder(orderId);
        return new ResponseUtil<String>().setData("成功", "success");
    }

}
